package com.app.view.component.room 
{
	import flash.utils.Timer;
	import flash.events.TimerEvent;
	import flash.display.MovieClip;
	import flash.geom.Point;
	
	import com.greensock.TweenLite;
	
	import com.peachcastle.core.Display;
	
	import com.app.view.component.room.dice.ShakerDice;
	
	/**
	 * ...
	 * @author kevin
	 */
	public class ShakerDiceItem extends Display
	{
		private var _frist:Point = null;
		private var _centerPoint:Point = null;
		
		private var _dices:Array =  null;
		private var _diceView:MovieClip = null;
		
		private var _timer:Timer = null;
		private var _frame:int = 1;
		
		public function ShakerDiceItem(ui:MovieClip, p:Point) 
		{
			_centerPoint = p;
			
			super(ui);
			
		}
		
		override protected function initialization():void 
		{
			super.initialization();

			_diceView = assest["dices"];
			_dices = [];
			for (var i:int = 0; i < 3; i++)
			{
				_dices[_dices.length] = new ShakerDice(_diceView['dice' + i]);
			}
			
			_frist = new Point(assest.x, assest.y);
			
			assest.gotoAndStop("shakerstop");	
			
			_timer = new Timer(31);
		}
		
		override protected function configEventListeners():void 
		{
			super.configEventListeners();

			assest.addFrameScript(assest.totalFrames - 1, onLastFrames);
			assest.addFrameScript(45, onShakeStop);
			assest.addFrameScript(21, onVisibleDice);
		}
		
		private function onTimer(event:TimerEvent):void
		{
			var f:int  = (_frame + _timer.currentCount) % (assest.totalFrames + 1);
			assest.gotoAndStop(f);
			
		}
		
		public function showValue(data:Array = null):void
		{
			for (var i:int = 0; i < _dices.length; i++)
			{
				if (data[i] != null)
				{
					ShakerDice(_dices[i]).show(data[i]);
				}
			}
			
			play("open");
		}
		
		public function play(status:String):void
		{
			switch(status)
			{
				case "open":
					TweenLite.to(assest, .6, {x:_centerPoint.x, y:_centerPoint.y, scaleX:2, scaleY:2, onComplete:onOpenEnd});
				
					break;
					
				case "start":
					reset();
					assest.gotoAndPlay("shaker");

					break;
			}
		}
		
		private function onShakeStop():void
		{
			assest.stop();
			_diceView.visible = true;
			_timer.stop();
		}
		
		private function onOpenEnd():void
		{
			_frame = 47;
			assest.gotoAndPlay("open");
		}
		
		private function onLastFrames():void
		{
			assest.stop();
			TweenLite.delayedCall(1, onMoveEnd);			
		}
		
		private function onMoveEnd():void
		{
			TweenLite.to(assest, .3, {x:_frist.x, y:_frist.y, scaleX:1, scaleY:1});
		}
		
		private function onVisibleDice():void
		{
			_diceView.visible = false;
		}
		
		public function reset():void
		{
			assest.gotoAndStop("shakerstop");
			assest.x = _frist.x;
			assest.y = _frist.y;
			assest.scaleX = assest.scaleY = 1;
		}
	}

}